:******************************************************************************
:* BCPIN.CMD
:******************************************************************************
:* BCP Import Script
:******************************************************************************
:* Developer: Dmitry Frenkel dima.frenkel@ps.net
:* Tenet Application Architecture Team
:* (C) 2003, Perot Systems Corporation. All rights reserved.
:*
:*	Modified By: Tanya Romankova 
:*	Modified On: 10/21/2003
:*	Modification: Add parameters for DB user authorization. 
:*  Modification: Cash Foley 12/07/2003 - Add AutoIncrement ReSeed
:******************************************************************************
:* Usage:
:* bcpin <ServerName> <Database> <Owner> <TableName> <Mode> <DestinationFile> [<UserName> [<Password>]]
:* 1 <ServerName> - name of the database server
:* 2 <Database> - name of the database
:* 3 <Owner> - owner of the object
:* 4 <TableName> - name of table (or view) to import data
:* 5 <Mode> - import mode: N=native,C-character. 
:* 6 <DestinationFile> - Data File
:* 7 <UserName> - DB user name (empty for trusted connection)
:* 8 <Password> - DB user password (empty for trusted connection)
:******************************************************************************
:* Script uses informationm provided to launch SQL Server bcp tool,
:* import data into designated table from file <DestinationFile>
:******************************************************************************
:* Requires - bcp.exe
:******************************************************************************
:* Notes: Script uses trusted connection to log into SQL server
:******************************************************************************

@ECHO OFF
: 1. We need to have all parameters present
IF "%1" == "" GOTO ERRORPARAMS
IF "%2" == "" GOTO ERRORPARAMS
IF "%3" == "" GOTO ERRORPARAMS
IF "%4" == "" GOTO ERRORPARAMS
IF "%5" == "" GOTO ERRORPARAMS
IF "%6" == "" GOTO ERRORPARAMS

: 2. Use DB authorization or trusted connection
SET Login= 
IF "%7" == ""  (
    SET Login=-T
    SET TargetServerSQL=%1 -E
) ELSE (
    IF "%8" == "" GOTO ERRORPARAMS
    SET Login=-U%7 -P%8
    SET TargetServerSQL=%1 -U%7 -P%8
)

: 3. We need to determine export mode and run bcp in this mode
SET Charset = 
IF /I "%5" == "C" (
    SET Charset=-c -CRAW
) ELSE (
    IF /I "%5" == "N" (
        SET Charset=-n
    ) ELSE (
        GOTO ERRORPARAMS
    )
)

: 4. Call bcp 
@echo Loading %4...
BCP %2.%3.%4 in %6 %Charset% %Login% -q -v -E -S%1

osql -S %TargetServerSQL% -d %2 -Q"DBCC CHECKIDENT('%4', RESEED, -1)"
osql -S %TargetServerSQL% -d %2 -Q"DBCC CHECKIDENT('%4', RESEED)"

GOTO END

:ERRORPARAMS
@ECHO  Usage: bcpin ServerName Database Owner TableName Mode DestinationFile [UserName [Password]]
@ECHO  [ServerName] - name of the database server
@ECHO  [Database] - name of the database
@ECHO  [Owner] - owner of the object
@ECHO  [TableName] - name of table (or view) to import data
@ECHO  [Mode] - import mode: N-native, C-character 
@ECHO  [DestinationFile] - Data File
@ECHO  [UserName] - DB user name (empty for trusted connection)
@ECHO  [Password] - DB password (empty for trusted connection)
:END